//
//  SEGBillPaymentTableViewCell.m
//  SEGUhomePropertyBaseService
//
//  Created by Clavis on 2019/3/6.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGBillPaymentTableViewCell.h"

@interface SEGBillPaymentTableViewCell ()

@property (nonatomic, strong) UIButton *radioButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *feeLabel;
@property (nonatomic, strong) UILabel *leftFeeLabel;
@property (nonatomic, strong) UIImageView *dashLine;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation SEGBillPaymentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.radioButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(radioButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select_nor")] forState:UIControlStateNormal];
        [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select")] forState:UIControlStateSelected];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 37, 0, 0);
        [self.contentView addSubview:button];
        button;
    });
    [self.radioButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(60);
        make.height.mas_greaterThanOrEqualTo(50);
    }];
    
    self.titleLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self.contentView addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = kFont_13;
        lable;
    });
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(60);
        make.top.mas_equalTo(19);
        make.bottom.mas_equalTo(-19);
        make.width.mas_lessThanOrEqualTo((LYScreenWidth() - 68) * 0.7);
    }];
    
    self.feeLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self.contentView addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = kFont_13;
        lable;
    });
    [self.feeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.offset(-10);
        make.width.mas_lessThanOrEqualTo((LYScreenWidth() - 68) * 0.3);
    }];
    
    self.leftFeeLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self.contentView addSubview:lable];
        lable.textColor = kColor_3;
        lable.font = kFont_11;
        lable;
    });
    [self.leftFeeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.offset(10);
        make.width.mas_lessThanOrEqualTo((LYScreenWidth() - 60) * 0.3);
    }];
    
    // 画虚线
    self.dashLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, LYScreenWidth() - 52, 1)];
    [self addSubview:self.dashLine];
    UIGraphicsBeginImageContext(self.dashLine.frame.size); //参数size为新创建的位图上下文的大小
    [self.dashLine.image drawInRect:CGRectMake(0, 0, LYScreenWidth() - 52, 1)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapSquare); //设置线段收尾样式
    CGFloat length[] = {3,1.5}; // 线的宽度，间隔宽度
    CGContextRef line = UIGraphicsGetCurrentContext(); //设置上下文
    CGContextSetStrokeColorWithColor(line, kColor_L2.CGColor);
    CGContextSetLineWidth(line, 1); //设置线粗细
    CGContextSetLineDash(line, 0, length, 2);//画虚线
    CGContextMoveToPoint(line, 0, 0.0); //开始画线
    CGContextAddLineToPoint(line, LYScreenWidth() - 52, 0.0);//画直线
    CGContextStrokePath(line); //指定矩形线
    self.dashLine.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.dashLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
        make.left.mas_equalTo(37);
        make.right.mas_equalTo(-15);
    }];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = kColor_L2;
    self.lineView.hidden = YES;
    [self addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.height.mas_equalTo(0.5);
    }];
    
}

- (void)updateUIWithModel:(SEGOwesMonthDetailModel *)model isHaveRadio:(BOOL)isHaveRadio {
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(isHaveRadio ? 60 : 37);
        make.top.mas_equalTo(19);
        make.bottom.mas_equalTo(-19);
        make.width.mas_lessThanOrEqualTo((LYScreenWidth() - 68) * 0.7);
    }];
    
    if ([model.lfree doubleValue] > 0) {
        [self.feeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.centerY.offset(-10);
        }];
        self.leftFeeLabel.hidden = NO;
    } else {
        [self.feeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.centerY.offset(0);
        }];
        self.leftFeeLabel.hidden = YES;
    }
    
    self.radioButton.hidden = !isHaveRadio;
    self.radioButton.selected = model.isSelected;
    self.titleLabel.text = model.feeItemTypeName;
    CGFloat fee = (fabs(model.fee.doubleValue) + fabs( model.lfree.doubleValue)) * MoneyRateChange;
    self.feeLabel.text = [NSString stringWithFormat:@"%.2f", fee];
    self.leftFeeLabel.text = [NSString stringWithFormat:@"含违约金%.2f", model.lfree.doubleValue * MoneyRateChange];
}

- (void)showDashLine:(BOOL)isDash {
    self.dashLine.hidden = !isDash;
    self.lineView.hidden = isDash;
}

- (void)radioButtonTap:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.changeRadioBlock) {
        self.changeRadioBlock(sender.selected);
    }
}

@end
